#Include Once "mud-gui/widgets/IntSpinBox.bi"

Namespace ui
	
	constructor IntSpinBox ( byval parent as BaseWidget ptr = 0, ByVal _val_ As integer = 0, byval _step_ as integer = 1 )

		if parent <> 0 then
			if parent->cl <> layout_t.canHaveLayout then ShowError("IntSpinBox widget must have a valid parent.")
			SetParent(parent)

		end if
		setMinimum = -32000
		setMaximum = 32000
		setStep_ = _step_
		setRollover = 1
		(BaseObject).id = ui.widgetids.IntSpinBox
		(BaseObject).cl = layout_t.canHaveLayout

		connectSignal(@This,resize,IntSpinBox.resize_handler)

		m_gl = New GridLayout( toBaseWidget(@This), 6, 2 )
		m_le = New LineEdit( ,Str(_val_) )
		m_le->setEditable = FALSE 

		m_up = New PushButton(, "^")
		m_down = New PushButton(,"v")


		connectSignal( m_up, clicked, IntSpinBox.m_up_clicked )
		connectSignal( m_down, clicked, IntSpinBox.m_down_clicked )

		m_gl->PlaceWidget( toBaseWidget(m_le), 1, 1, 5, 2 )
		m_gl->PlaceWidget( toBaseWidget(m_up), 6, 1 )
		m_gl->PlaceWidget( toBaseWidget(m_down), 6, 2 )

	End Constructor

	IMP_PROPERTY( IntSpinBox, step_, integer )
	IMP_PROPERTY( IntSpinBox, minimum, integer )
	IMP_PROPERTY( IntSpinBox, maximum, integer )
	IMP_PROPERTY( IntSpinBox, rollover, ubyte )
	
	property IntSpinBox.value ( ) As Integer
		Return ValInt(m_le->text)
	End property
	
	property IntSpinBox.setValue( byval _t_ as integer )
		m_le->setText = str(_t_) 
	end property

	STATIC_SLOT(IntSpinBox.resize_handler)
		Var z = Cast(IntSpinBox Ptr, ref )
		z->m_gl->___base.width = z->___base.width
		z->m_gl->___base.height = z->___base.height
		z->m_gl->___base.x = z->___base.x
		z->m_gl->___base.y = z->___base.y
		If z->m_gl->m_grid <> 0 Then DeAllocate z->m_gl->m_grid
		z->m_gl->sizeGrid( 6, 2 )
		Return 0
	Endslot

	STATIC_SLOT(IntSpinBox.m_up_clicked)
		Cast( PushButton ptr, ref )->m_isdown = 0
		line ref->buffer, (0,0)-(ref->width,ref->height), rgb(&hc9,&hc9,&hc9 ), BF


		Var pz = ref->Parent
		if pz = 0 then ShowError("Parent must not be null for child widget. l69")
		if pz->Parent = 0 then ShowError("Parent must not be null for child widget. l70")
		if pz->Parent->id <> widgetids.intspinbox then ShowError("WID does not match expected.")
		var z = cast( IntSpinBox ptr, pz->Parent )
		var m_max = z->maximum
		var m_min = z->minimum
		var m_step = z->step_
		Var curval = z->value
		if z->rollover = 0 then
			If curval <= m_max Then z->setValue = iif((curval + m_step)>m_max,m_max,curval+m_step) 
		else
			If curval <= m_max Then z->setValue = iif((curval + m_step)>m_max,m_min,curval+m_step) 
		end if


		Return 1
	Endslot

	STATIC_SLOT(IntSpinBox.m_down_clicked)
		Cast( PushButton ptr, ref )->m_isdown = 0
		line ref->buffer, (0,0)-(ref->width,ref->height), rgb(&hc9,&hc9,&hc9 ), BF


		Var pz = ref->Parent
		if pz = 0 then ShowError("Parent must not be null for child widget. l86")
		if pz->Parent = 0 then ShowError("Parent must not be null for child widget. l87")
		if pz->Parent->id <> widgetids.intspinbox then ShowError("WID does not match expected.")
		var z = cast( IntSpinBox ptr, pz->Parent )
		var m_max = z->maximum
		var m_min = z->minimum
		var m_step = z->step_
		Var curval = z->value
		if z->rollover = 0 then
			If curval >= m_min Then z->setValue = iif((curval - m_step)<m_min,m_min,curval-m_step) 
		else
			If curval >= m_min Then z->setValue = iif((curval - m_step)<m_min,m_max,curval-m_step) 
		end if

		Return 1
	Endslot


End Namespace
